package org.extremengine.common;

/**
 * 物理世界中的，以左下角点为原点，水平线向右为x轴，垂线向上为y轴的二维坐标轴中的一个坐标点。
 * @author yuxing
 *
 */
public class Point {
	
	// Be public for short
	public double x;
	public double y;

	public Point(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	/*
	 * Get the center point between me and another point.
	 */
	public Point centerPoint(Point antherPoint) {
		return new Point(x + (antherPoint.x - x) / 2, y + (antherPoint.y - y) / 2);
	}
	
	/**
	 * Get distance from me to another point.
	 * @param antherPoint
	 * @return
	 */
	public double distanceTo(Point antherPoint) {
		double deltay = (antherPoint.y - y);
		double deltax = (antherPoint.x - x);
		return Math.hypot(deltax, deltay);
	}
	
	/**
	 * Calculate the angle degree in the coordinate from this point to another point
	 * @param anotherPoint
	 * @return
	 */
	public double angleDegreeToPoint(Point anotherPoint) {
		double deltax = (anotherPoint.x - x);
		double deltay = (anotherPoint.y - y);
		double distance = Math.hypot(deltay, deltax);
		double sin_a = deltay / distance;
		double cos_a = deltax / distance;
		System.out.println(Math.toDegrees(Math.asin(sin_a)) + "  " + Math.toDegrees(Math.acos(cos_a)));
		if(deltax > 0) {
			// #1
			if(deltay > 0) {
				return Math.toDegrees(Math.asin(sin_a));
			}
			// #4
			else {
				return Math.toDegrees(Math.asin(sin_a));
			}
		}
		else {
			// #2
			if(deltay > 0) {
				return Math.toDegrees(Math.acos(cos_a));
			}
			// #3
			else {
				return Math.abs(Math.toDegrees(Math.asin(sin_a))) + 180; 
			}
			
		}
	}

	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(double y) {
		this.y = y;
	}

	@Override
	public String toString() {
		return "[P:" + x + ", " + y + "]";
	}

}
